#include "QBoolProperty.h"

#include <QCheckBox>
#include <QApplication>
#include <QStyle>
#include <QSizePolicy>
#include <QObject>
#include <QSpinBox>

#include "private/QProperty_p.h"

using namespace QPE;

QVariant QBoolProperty::TrueText;
QVariant QBoolProperty::FalseText;

QBoolProperty::QBoolProperty(const QString& name) :
	QProperty(name, QVariant::Bool)
{
	d_ptr->VariantValue.setValue(false);
	d_ptr->VariantValue.convert(QVariant::Bool);

	// I'm not sure, how Qt handles the translations...
	if(TrueText.isNull()) TrueText = QObject::tr("True");
	if(TrueText.isNull()) FalseText = QObject::tr("False");
}


//! Get the data how it should be displayed
QVariant QBoolProperty::data (int column, int role) const
{
	if(column == DPC_Data && (Qt::DisplayRole == role || Qt::EditRole == role))
		return d_ptr->VariantValue.toBool() ? TrueText : FalseText;
	if(column == DPC_Data && Qt::CheckStateRole == role)
		return d_ptr->VariantValue.toBool() ? Qt::Checked : Qt::Unchecked;
	else
		return QProperty::data(column, role);
}

bool QBoolProperty::setData(const QVariant &data, int role)
{
	if(Qt::CheckStateRole == role)
	{
		d_ptr->VariantValue = (Qt::Checked == static_cast<Qt::CheckState>(data.toInt())); return true;
	}

	return false;
}

//! Returns item flags
Qt::ItemFlags QBoolProperty::flags(int column) const
{
	if(column == DPC_Data)
		return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
	else
		return QProperty::flags(column);
}

QString QBoolProperty::type() const
{
	return "bool";
}

QProperty *QBoolProperty::clone(bool include_children, QProperty *container) const
{
	return QProperty::clone(include_children, container ? container : new QBoolProperty(getName()));
}
